window.ROOT = "";
(function() {
	var roleId = jQuery("#id").val();
	// //////////////初始化所有菜单的树结构/////////////////////////
	var modalSetting = {
		edit : {
			enable : true,
			showRemoveBtn : false,
			showRenameBtn : false
		},
		data : {
			simpleData : {
				enable : true,
				idKey : "id",
				pIdKey : "parentId",
				rootParentId : 0
			}
		},
		check : {
			enable : true,
			chkStyle : "checkbox",
		},
		view : {
			showLine : true,
		},
	};
	///初始化角色的被授权的菜单树
	var myModalSetting = {
			 edit:{
		            enable: true,
		            showRemoveBtn:setRemoveBtn,
		            showRenameBtn:false
		        },
		        data: {
		            simpleData: {
		                enable: true,
		                idKey: "id",
		                pIdKey: "parentId",
		                rootParentId: 0
		            }
		        },
		        view:{
		            showLine:true
		        },
		        callback:{
		            beforeRemove: beforeRemove,
		            onRemove:onRemove,
		            onClick:editOper
		        }

		};

	var menuId = "1";
	var primaryNode = [];
	var url = window.rootPath+'/ui/module/getAllNodes';
	jQuery(".btn").attr("disabled", true);
	jQuery.ajax({
		url : url,
		cache : false,
		type : "get",
		data : {
			"menuId" : menuId
		},
		dataType : "json",
		contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
		success : function(data) {		
			primaryNode.push({
				id : 1,
				name : "模型树",
				open : "true"
			});
			for (var i = 0; i < data.nodes.length; i++) {
				primaryNode.push(data.nodes[i]);
			}
			$.fn.zTree.init($("#moduleTree"), modalSetting, primaryNode);
		     /////加载角色的菜单
		    jQuery.ajax({
				url : window.rootPath+'/ui/m2r/getRoleNodes',
				cache : false,
				type : "get",
				data : {
					"roleId" : roleId
				},
				dataType : "json",
				contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
				success : function(data) {
					jQuery(".btn").attr("disabled", false);
					var roleNodes=[];
					var menuitree = $.fn.zTree.getZTreeObj("moduleTree")
					roleNodes.push({
						id : 1,
						name : "模型树",
						open : "true"
					});
					for (var i = 0; i < data.nodes.length; i++) {
						roleNodes.push(data.nodes[i]);
						var nodeId=data.nodes[i].id;
						var node = menuitree.getNodeByParam("id", nodeId,null);
						 menuitree.selectNode(node);
			             node.checked=true;
			             menuitree.updateNode(node); 
							
					}
					$.fn.zTree.init($("#myModuleTree"), myModalSetting, roleNodes);	
				}
			});
			
		}
	});

	
	//删除按钮的设置
    function setRemoveBtn(treeId, treeNode) {
        //判断为顶级节点则不显示删除按钮
        if(treeNode.level == 0)
            return false;
        else
            return true;
    }
   
    //////////////////删除节点/////////////////////////////
    function beforeRemove(treeId, treeNode) {
        return confirm("确认删除 " + treeNode.name + "菜单吗？");
    }
    ////编辑授权模块的操作权限
    function editOper(e, treeId, treeNode){
    	var modalId=treeNode.id;
    	jQuery("#modelId").val(modalId);
    	var roleId = jQuery("#id").val();
    	var url=window.rootPath+'/ui/m2r/getOperateModal';
    	jQuery.ajax({
            url:encodeURI(url),
            cache:false,
            type :"get",
            data:{
                "modalId":modalId,
                "roleId":roleId
            },
            dataType:"json",
            contentType:"application/json;charset=utf-8",//解决返回数据中的中文乱码问题
            success:function(data){
            	if(data.read=='true'){
            		$("#readyOnly").prop("checked","checked");
            	}else{
            		$("#readyOnly").prop("checked",false);
            	}
            	if(data.add=='true'){
            		$("#addOnly").prop("checked","checked");
            	}else{
            		$("#addOnly").prop("checked",false);
            	}
            	if(data.modify=='true'){
            		$("#modifyOnly").prop("checked","checked");
            	}else{
            		$("#modifyOnly").prop("checked",false);
            	}
            	if(data.del=='true'){
            		$("#deleteOnly").prop("checked","checked");
            	}else{
            		$("#deleteOnly").prop("checked",false);
            	}
            	if(data.other1=='true'){
            		$("#other1Only").prop("checked","checked")
            	}else{
            		$("#other1Only").prop("checked",false);
            	}
            	if(data.other2=='true'){
            		$("#other2Only").prop("checked","checked")
            	}else{
            		$("#other2Only").prop("checked",false);
            	}
            	showSelectOption("#dataFilter",data.dataFilter);
            	jQuery('#operateSetModal').modal('show');
            }
        });
    }
   /**
    * 保存模块的操作权限
    */
    jQuery('.saveOperateBtn').on('click',function(){
    	var readonly=jQuery("#readyOnly").prop("checked");
    	var modifyonly=jQuery("#modifyOnly").prop("checked");
    	var addonly=jQuery("#addOnly").prop("checked");
    	var deleteonly=jQuery("#deleteOnly").prop("checked");
    	var other1only=jQuery("#other1Only").prop("checked");
    	var other2only=jQuery("#other2Only").prop("checked");
    	var modalId=jQuery("#modelId").val();
    	var roleId = jQuery("#id").val();
    	var dataFilter=jQuery('#dataFilter').val();
    	var url=window.rootPath+'/ui/m2r/setModalOpert';
    	jQuery(".btn").attr("disabled", true);
    	jQuery.ajax({
    		url:encodeURI(url),
            cache:false,
            type :"post",
            data:{
            	"roleId":roleId,
                "modalId":modalId,
                "readonly":readonly,
                "delonly":deleteonly,
                "addonly":addonly,
                "modifyonly":modifyonly,
                "other1only":other1only,
                "other2only":other2only,
                "dataFilter":dataFilter
            },
            success:function(data){
            	jQuery(".btn").attr("disabled", false);
            	jQuery('#operateSetModal').modal('hide');
            }
        });
    })
    
    ////删除角色绑定的菜单//////////////
    function onRemove(e, treeId, treeNode) {
        //此处treeNode 为当前节点,
        var str ='' ;
        var selectPId=treeNode.getParentNode().id;
        var roleId = jQuery("#id").val();
        var url=window.rootPath+'/ui/m2r/deleteRoleNode';
        str=treeNode.id;
        str = getAllChildrenNodes(treeNode,str);//获选定的菜单的id和子菜单id的字符串
        jQuery(".btn").attr("disabled", true);
        jQuery.ajax({
            url:encodeURI(url),
            cache:false,
            type :"get",
            data:{
                "str":str,
                "selectPId":selectPId,
                "roleId":roleId
            },
            dataType:"json",
            contentType:"application/json;charset=utf-8",//解决返回数据中的中文乱码问题
            success:function(data){
            	jQuery(".btn").attr("disabled", false);
            }
        });
    }
   
    
    
	// 授权按钮，将选中的模块配置给当前角色
	jQuery(".setModuleBtn").on("click",function() {
				var roleId = jQuery("#id").val();
				///获得选择的菜单的id集合
				var nodesId=[];
				
				var myNodes=[];
				var treeObj = $.fn.zTree.getZTreeObj("moduleTree");
				var nodes = treeObj.getCheckedNodes(true);
				for (var i = 0; i < nodes.length; i++) {
					if(nodes[i].id!=1){
						nodesId.push(nodes[i].id);
					}	
				}
				//////获得角色的id集合
				var roleTree=$.fn.zTree.getZTreeObj("myModuleTree");
				var roleNodes=roleTree.getNodes();
				var	nodes_array = roleTree.transformToArray (roleNodes);
				var roleNodesId=[];
				for (var i = 0; i < nodes_array.length; i++) {
					if(nodes_array[i].id!=1){
						roleNodesId.push(nodes_array[i].id);
					}	
				}
				////获得要增加的节点id， nodesId-roleNodesId
				var addNodesId=[];
				addNodesId=array_difference(nodesId, roleNodesId);
				////获得要删除的节点id，roleNodesId-nodesId
				var removeNodesId=[];
				removeNodesId=array_difference(roleNodesId,nodesId);
				$.springLemonDialog({
					content : '确认配置所勾选模块？',
					clickButton : function(sender, modal, index) {
						if (index == 0) {
							var url = window.rootPath + '/ui/m2r/setNodes';
							jQuery(".btn").attr("disabled", true);
							jQuery.ajax({
								url : encodeURI(url),
								type : 'get',
								cache : false,
								data : {
									"addNodesId" : addNodesId,
									"removeNodesId" : removeNodesId,
									"roleId" : roleId
								},
								dataType : 'json',
								contentType : 'application/json;charset=utf-8',
								success : function(data) {
									jQuery(".btn").attr("disabled", false);
									myNodes.push({
										id : 1,
										name : "模型树",
										open : "true"
									});
									for (var i = 0; i < data.nodes.length; i++) {
										myNodes.push(data.nodes[i]);
									}
									$.fn.zTree.init($("#myModuleTree"),myModalSetting,myNodes);	
								}
							});
						}
						$(this).closeDialog(modal);
					}
				});
			});

})();
